﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;

namespace TableParsers
{
    [Serializable]
    public class AttendanceTableEntry
    {
        public bool Present = false;
        public ClassTime ClassTime = null;
        public ClassName ClassName = null;
        public String Course = null;
        public int Unit = 0;
    }

    [Serializable]
    public class AttendanceTable : List<AttendanceTableEntry>
    {
        public static AttendanceTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebPageParser.WebPageCache pageCache = new WebPageParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        public static AttendanceTable Fetch(WebPageParser.WebPageCache pageCache)
        {
            WebPageParser.ClassesWebPage webPage = pageCache.GetPage<WebPageParser.ClassesWebPage>();

            AttendanceTable attendanceTable = new AttendanceTable();
            foreach (WebPageParser.ClassesTableEntry webEntry in webPage.AttendanceTableContent)
            {
                AttendanceTableEntry memEntry = new AttendanceTableEntry();

                memEntry.ClassName = ClassNameParser.ParseClassName(webEntry.Type);
                memEntry.ClassTime = ClassTimeParser.ParseAttendedEntry(webEntry.Date,webEntry.Begin,webEntry.End);
                memEntry.Course = webEntry.Course;

                if (Int32.TryParse(webEntry.Unit, out memEntry.Unit) == false)
                {
                    memEntry.Unit = 0;
                }

                memEntry.Present = webEntry.Presence == "yes";

                attendanceTable.Add(memEntry);
            }

            return attendanceTable;
        }
    
    }
}
